const app = getApp()
Page({
  data: {
    loading: true, 
    show: false,
    details:true,
    show_follow:true,
    onLike:false,
    keyheight:0,
    textarea:'',
    uid:0,
    key:0,
    info_id: 0,
    page: 0,
    item: [],
    reply: []
  },
  //载入
  onLoad: function (options) {
    app.getParam(options,(rel)=>{
      this.setData({
        info_id: rel.id,
        show:app.util().isNull(rel.reply)?false:true
      })
      this.getView();
      this.getReply();
    });
  },
  //登录的UID
  onShow:function(){
    if(!app.util().isNull(app.globalData.loginuser)){
      this.setData({
        uid:app.globalData.loginuser.uid
      });
    }
  },
  // 下拉刷新
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    //停止下拉刷新
    setTimeout(function () {
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
    }, 1500);
    this.setData({
      page: 0,
      loading: true,
      reply: []
    });
    this.getReply();
  },
  //上拉加载
  onReachBottom: function () {
    this.setData({
      loading: true,
    });
    this.getReply();
  },
  //获取首页接口
  getView: function () {
    app.api().Get('api/v1/ais/info/getview', {info_id:this.data.info_id},(result)=>{
      this.setData({
        item: result.data,
      });
    })
  },
  //获取首页接口
  getReply: function () {
    if (this.data.loading) {
      //获取推荐接口
      var param = {
        page: this.data.page + 1,
        id:this.data.info_id,
      }
      app.api().Get('api/v1/ais/info/reply',param,(result)=>{
        if (result.code == 200) {
          var reply = this.data.reply;
          for (let i in result.data) {
            reply.push(result.data[i]);
          }
          this.setData({
            reply: reply,
            page: param.page,
          });
        }
        this.setData({
          loading: false,
        });
      })
    }
  },
  //发布评论
  postInfo: function (e) {
    var data = e.detail.value;
    if (app.util().isNull(data.content)) {
      app.wxAlert('内容不能为空')
      return;
    }else{
      wx.showLoading({title:'违规内容检测',mask: true})
      this.doMsgSecCheck(data.content,(res)=>{
        wx.hideLoading();
        if(res){
          app.wxAlert('内容中有违规词语,请更换');
        }else{
          this.setData({disabled:true})
          app.api().Post('api/v1/ais/infosend/postreply',{
            content: data.content,
            info_id: this.data.info_id,
          },(res)=>{
            wx.showToast({title:'已评论',icon: 'success'})
            setTimeout(() =>{wx.hideToast()},2000)
            this.setData({
              show: false,
              loading: true,
              disabled:false,
              page: 0,
              reply: [],
              textarea:''
            });
            this.getReply();
          })
        }
      })
    }
  },
  //点赞
  onLike:function(e){
    let id = e.currentTarget.dataset.id;
    var item = this.data.item;
    if (item.is_like == 0){
      app.api().Post("api/v1/ais/infosend/like",{id:id},(rel) => {
        item.is_like = 1;
        item.like    = item.like+1;
        this.setData({
          item:item,
          onLike:true
        })
        setTimeout(()=>{
          this.setData({
            onLike:false
          })
        },600)
      })
    }
  },
  onReplyLike:function(e){
    let key = e.currentTarget.dataset.key,id = e.currentTarget.dataset.id;
    var reply = this.data.reply;
    if (reply[key].is_like == 0){
      app.api().Post("api/v1/ais/infosend/replylike",{id:id},(rel) => {
        reply[key].is_like = 1;
        reply[key].like    = reply[key].like+1;
        this.setData({
          reply:reply,
          onLike:true
        })
        setTimeout(()=>{
          this.setData({
            onLike:false
          })
        },600)
      })
    }
  },
  //关注与取消
  onFollow:function (e) {
    var item = this.data.item;
    if (item.is_follow == 0){
      app.api().Post("api/v1/ais/infosend/follow",{uid:item.user_id,state:1},(rel) => {
        item.is_follow = 1;
        this.setData({
          item:item
        })
      })
    }
  },
  //发起支付
  dopay:function (e) {
    wx.navigateTo({
      url: 'on_order?id='+this.data.info_id,
    })
  },
  //底部弹窗
  toggleBottomPopup: function () {
    this.setData({
      show: !this.data.show
    });
  },
  //跳到首页
  reactHome:function() {
    wx.switchTab({
      url: '../index',
    })
  },
  //拨打电话
  callPhone:function (e) {
    app.callPhone(e);
  },
  //图片预览
  previewImage: function (e) {
    wx.previewImage({
      current: this.data.item.img,
      urls: this.data.item.images
    })
  },
  //用户点击右上角分享
  onShareAppMessage: function (res) {
    return {
      title: this.data.item.themes,
      path: '/packageInfo/pages/info/details?id=' + this.data.info_id+'&ucode=' + app.globalData.loginuser.ucode
    }
  },
  //键盘行为监听
  inputCommentsFocus: function(e){
    this.setData({
      keyheight:e.detail.height
    })
  },
  //市区键盘焦点
  inputCommentsBlur: function(e) {
    this.setData({
      keyheight:0
    })
  },
  //锁屏
  move: function () {

  },
  //文字内容安全
  doMsgSecCheck: function (text,callback) {
    wx.serviceMarket.invokeService({
      service: 'wxee446d7507c68b11',
      api: 'msgSecCheck',
      data: {
        "Action": "TextApproval",
        "Text":text
      },
    }).then(res => {
      if(res.errMsg == 'invokeService:ok'){
        if(res.data.Response.EvilTokens.length > 0){
          callback(true)
        }else{
          callback(false)
        }
      }
    })
  },
})